package ysf.com.mediumdifficulty;

/**
 * @author ysf
 * @date 2021/6/5 22:47
 */
public class SecondsDemo {

    public static void main(String[] args) {
        SecondListNode FnodeSta = new SecondListNode(2);  // 创建首结点  【第一个链表】
        SecondListNode FnextNode;           // 声明一个变量用来移动过程中指向当前结点
        FnextNode = FnodeSta;   // 指向首结点

        SecondListNode FnewNode = new SecondListNode(4);  // 创建新的结点
        FnextNode.next = FnewNode;           // 把新结点连接起来
        FnextNode = FnextNode.next;         // 把结点往后移

        SecondListNode FnewNodes = new SecondListNode(3);  // 创建新的结点下
        FnextNode.next = FnewNodes;        // 把新的结点连接起来
        FnextNode = FnewNode.next;        // 结点往后移

        FnextNode = FnodeSta;         // 重新指向首结点
        /*System.out.println("==============================the first ListNode BEGIN====================================");
        print(FnodeSta);
        System.out.println("==============================the first ListNode END====================================");

        System.out.println("===================================创建第二个链表=======================================");*/

        SecondListNode SnodeSta = new SecondListNode(5);  // 创建首结点    【第二个链表】
        SecondListNode SnextNode;        // 声明一个变量用来移动过程中指向当前结点
        SnextNode = SnodeSta;      // 指向首结点

        SecondListNode SnewNode = new SecondListNode(6);  // 创建新的结点
        SnextNode.next = SnewNode;                // 把新的结点连起来
        SnextNode = SnextNode.next;               //  结点往后移


        SecondListNode SnewNodes = new SecondListNode(4);   // 创建新的结点
        SnextNode.next = SnewNodes;                // 把新的结点连起来
        SnextNode = SnextNode.next;               //  结点往后移
        SnextNode = SnodeSta;                    // 指向首结点
        /*System.out.println("==============================the second ListNode 结果是 BEGIN====================================");
        print(SnodeSta);
        System.out.println("==============================the second ListNode 结果是 END====================================");


        ListNode ResultlistNode = addTwoNumbers(FnodeSta, SnodeSta);
        System.out.println("==============================计算后的结果是 BEGIN====================================");
        print(ResultlistNode);
        System.out.println("==============================计算后的结果是 END====================================");*/


        SecondListNode resultSecondListNodes = reverseListRecursion(FnodeSta);
        System.out.println("==============================链表逆序 BEGIN====================================");
        print(resultSecondListNodes);
        System.out.println("==============================链表逆序 END====================================");

    }
    // 输出各个结点的值
    public static void print(SecondListNode secondListNode) {
        while (secondListNode != null) {
            System.out.println("结点：" + secondListNode.val);
            secondListNode = secondListNode.next;
        }
        System.out.println();
    }

    // 将两个链表的数相加
    public static SecondListNode addTwoNumbers(SecondListNode secondListNode1, SecondListNode secondListNode2) {
        SecondListNode dumyHead = new SecondListNode(0);  // 头结点

        SecondListNode L1 = secondListNode1, L2 = secondListNode2, curr = dumyHead;

        int carry = 0;
        while (L1 != null || L2 != null) {
            int x = (L1 != null) ? L1.val : 0;
            int y = (L2 != null) ? L2.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new SecondListNode(sum % 10);
            curr = curr.next;
            if (L1 != null) L1 = L1.next;
            if (L2 != null) L2 = L2.next;
        }
        if (carry > 0) {
            curr.next = new SecondListNode(carry);
        }
        return dumyHead.next;
    }

    // 递归思想
    public static SecondListNode reverseListRecursion(SecondListNode head){
        SecondListNode newHead;
        if(head==null||head.next==null ){
            return head;
        }
        newHead=reverseListRecursion(head.next); // head.next 作为剩余部分的头指针
        System.out.println("NewHead is=====>"+newHead.val);
        head.next.next=head; //head.next 代表新链表的尾，将它的 next 置为 head，就是
        // 将 head 加到最后了。
        head.next=null;
        return newHead;
    }









}
